<?php
declare(strict_types=1);

namespace App\Common\Modular\Administer\Crontab;

use App\Common\Modular\Administer\Serve\LogServe;
use Core\HyperfUnit\Ip;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;

class LogCrontab
{
    #[Inject]
    protected LogServe $serve;

    /**
     * 设置IP属地,已自动任务模式运行
     */
    #[Crontab(rule: "*/10 * * * *", name: "logip.ascription", memo: "解析操作IP属地,每10分钟执行一次")]
    public function setLogAscription(): void
    {
        $log = $this->serve->search("`ascription`=''")->groupBy(['ip'])->select(['ip'])->limit(10)->get()->toArray();
        foreach ($log as $item) {
            $ascription = Ip::ascription($item['ip']) ?: '未知属地';
            $this->serve->search(['ip' => $item['ip']])->whereRaw("`ascription`=''")->update(['ascription' => $ascription]);
        }
    }
}
